/**
 ******************************************************************************
 * @file touch_screen.c
 * @author GX (2524913364@qq.com)
 * @brief 本模块适用于粤嵌的6818开发板的触摸屏
 * @version 2.0.0 
 * @date 2022-07-10
 * 
 * @copyright Copyright (c) 2022
 * 
 ******************************************************************************
 *----------------------------------------------------------------------------*
 *  Remark         : Description                                              *
 *----------------------------------------------------------------------------*
 *  Change History :                                                          *
 *  <Date>     | <Version> | <Author>       | <Description>                   *
 *----------------------------------------------------------------------------*
 *  2022-05-26 | 1.0.0     | GX             | Create file                     *
 *----------------------------------------------------------------------------*
 *  2022-07-10 | 2.0.0		 | GX							| 设计状态机判断触摸屏各种动作。       *
 * 						 |					 |								| 目前还不适合给用户使用，很多东西还没  *
 * 		         |					 |                | 整理。另外双击动作待添加            *
 ******************************************************************************
 */


/*----------------------------- start of file -------------------------------*/
#include "touch_screen.h"

TOUCH p;
TOUCH touch;	

/**
 * @brief 存储当前时间的变量，主要是为了给用户使用触摸屏状态机时使用的。
 * 用户需要时不时主动更新这个变量，将当前的时间跟状态机的时间作差，如果
 * 两者时间隔得比较长那就要更新状态机变量，下面有个例子。
 * 
 */
long tc_time_cur;

static pthread_t child_id;
static struct input_event tc_data;
static u_int32_t read_count = 0;
static bool is_double_click = false;
// static int 	fd_ts;	/* 触摸屏文件 */

/* 因为结构体 TOUCH 中记录时间的数值太大不直观，用这个变量减小一点比较直观 */
static long time_start;			

/**
 * @brief 下面演示了怎么使用这个模块，下面的函数是不能直接使用的
 */
#if 0	/* How2Use_mode */
void How2Use_mode(void)
{
	/*------------------------------显示有关，本本模块无关-----------------------------*/
	int fd_lcd;                             /* 打开文件获取文件描述 */
  int *fb_base = NULL;                    /* 映射后显存的地址 */
	/* 打开文件 */
	fd_lcd = open(LCD_PATH, O_RDWR);
	if(fd_lcd == -1)
	{
		perror("open lcd");
	}
  else
	{
		/* 显存映射 */
		fb_base = mmap(NULL, LCD_BUFF_SIZE*LCD_BLOCK,  PROT_READ|PROT_WRITE,
					MAP_SHARED, fd_lcd, 0);
		if(fb_base == MAP_FAILED) 
		{
			perror("申请显存失败");
		} 
		else
		{
			LCD_CLEAN(fb_base);
		}
	}
  DispStringLine_EN(fb_base, 1, 0, "STATE_NOT_PRESSED");


	/*-------------------------------触摸屏----------------------------------*/
  TOUCH *tc = malloc(......);
	InitTouch_pthread();	/* 开启触摸屏线程 */

  while(1)
  {
		get_tcState(tc);
    if( tc.response == true )
    {
      continue;
    }

    LCD_ClearLine(fb_base, 1);   

    switch (tc.state)
		{
			case STATE_NOT_PRESSED:	/* 无操作 */
        DispStringLine_EN(fb_base, 1, 0, "STATE_NOT_PRESSED");
        response_over();
				break;

			case STATE_PRESSED:			/* 按下 */
				DispStringLine_EN(fb_base, 1, 0, "STATE_PRESSED");
        response_over();
				break;

			case STATE_RELEASE:			/* 释放 */
				DispStringLine_EN(fb_base, 1, 0, "STATE_RELEASE");
        response_over();
				break;

      case STATE_SIN_OR_DOU:/* 判断单击还是双击 */
				DispStringLine_EN(fb_base, 1, 0, "STATE_SIN_OR_DOU");
        response_over();
				break;

			case STATE_SINGLE_CLICK:/* 单击 */
				DispStringLine_EN(fb_base, 1, 0, "STATE_SINGLE_CLICK");
        response_over();
				break;

			case STATE_DOUBLE_CLICK:/* 双击 */
				DispStringLine_EN(fb_base, 1, 0, "STATE_DOUBLE_CLICK");
        response_over();
				break;

			case STATE_SLIDING:				/* 滑屏 */
        DispStringLine_EN(fb_base, 1, 0, "STATE_SLIDING");
        response_over();
				break;
			
      case STATE_DTAGING:				/* 拖动 */
        DispStringLine_EN(fb_base, 1, 0, "STATE_DTAGING");
        response_over();
				break;

			default:
				break;
		}
  }
}
#endif	/* How2Use_mode */


pthread_t InitTouch_pthread(void)
{
	int ret;
	
	time_start = time(NULL);
	printf("time_start:%d\n", time_start);
	ret = pthread_create(&child_id, NULL,Touch_stateMachine, NULL);
	if(ret < 0)
	{
		perror("pthread create error");
		return -1;
	}
	return child_id;
}


/**
 * @brief 触摸屏状态机的线程中再次创建线程调用的函数
 * 				用来辅助判断双击动作
 * 
 * @param arg 
 * @return void* 
 */

// void *TouchGet(void *arg)
// {
// 	struct input_event ts_data;
	
// 	//读取触摸屏数据
// 	while(1)
// 	{
// 		for(int i=0;i<4;i++)
// 		{
// 			read(fd_ts, &ts_data, sizeof(struct input_event));
			
// 			if(ts_data.type==3 && ts_data.code==0)
// 			{
// 				is_double_click = true;
// 				touch.x_last = ts_data.value*800/1024;//*800/1024; 

// 				touch.time_last = (ts_data.time.tv_sec - time_start)*1.0
// 													+ ((ts_data.time.tv_usec/1000)*1.0/1000);
// 			}
// 			else if(ts_data.type==3 && ts_data.code==1)
// 			{
// 				is_double_click = true;
// 				touch.y_last = ts_data.value*480/600;//*480/600;

// 				touch.time_last = (ts_data.time.tv_sec - time_start)*1.0
// 													+ ((ts_data.time.tv_usec/1000)*1.0/1000);
// 			}
			
// 		}		
// 		// printf("\nlast(%d,%d)\n",p.x_last,p.y_last);	
// 		// printf("\n      (%d,%d)\n",p.x,p.y);	
// 		// printf("value:%d\n", ts_data.value);
// 		// printf("sec:%ld\tusec:%ld\n",ts_data.time.tv_sec, ts_data.time.tv_usec);
// 	}

// }


#if 0
void *TouchSlide(void *arg)
{
	struct input_event ts_data;
	int fd_ts;
	//打开触摸屏 
	fd_ts = open(TOUCH_PATH, O_RDONLY);
	
	printf("-------in-------\n");
	while(1)
	{
		p.x = p.y = p.x_last = p.y_last = 0;
		//读取触摸屏数据
		while(1)
		{
				printf("-------wait for touch-------\n");
				/* 读取触摸屏文件的数据 */
				read(fd_ts, &ts_data, sizeof(struct input_event));
				/* 发生触屏事件，且为x轴事件 */
				if(ts_data.type==EV_ABS && ts_data.code==ABS_X)
				{
					p.x = ts_data.value*800/1024;//*800/1024; 
				}

				/* 发生触摸屏事件，且为y轴事件 */
				else if(ts_data.type==EV_ABS && ts_data.code==ABS_Y)
				{
					p.y = ts_data.value*480/600;//*480/600;
				}

				if( p.x != 0 && p.y != 0)
				{
					printf("-------first touch-------\n");
					break;
				}
			// printf("\nlast(%d,%d)\n",p.x_last,p.y_last);	
			
		//	printf("sec:%ld\tusec:%ld\n",ts_data.time.tv_sec, ts_data.time.tv_usec);
		}
		/* 获取松开时的坐标 */
		while(1)
		{
				printf("-------wait for relse-------\n");
				/* 读取触摸屏文件的数据 */
				read(fd_ts, &ts_data, sizeof(struct input_event));

				/* 发生触屏事件，且为x轴事件 */
				if(ts_data.type==EV_ABS && ts_data.code==ABS_X)
				{
					p.x_last = ts_data.value*800/1024;//*800/1024; 
				}

				/* 发生触摸屏事件，且为y轴事件 */
				else if(ts_data.type==EV_ABS && ts_data.code==ABS_Y)
				{
					p.y_last = ts_data.value*480/600;//*480/600;
				}

				/* 手松开的时候 */
				if(ts_data.type==EV_KEY && ts_data.code==BTN_TOUCH && ts_data.value==0)
				{
					printf("-------relse touch-------\n");
					break;
				}
				
		}
		printf("\n按下(%d,%d)\n",p.x,p.y);	
		printf("松开(%d,%d)\n",p.x_last,p.y_last);	

		int deltaX = (p.x-p.x_last > 0) ? (p.x-p.x_last) : (p.x_last-p.x);
		int deltaY = (p.y-p.y_last > 0) ? (p.y-p.y_last) : (p.y_last-p.y);

		printf("差值");
		if( (deltaX > 20) && (p.x > p.x_last) && (deltaX > deltaY) )
		{
			p.motion = TC_LEFT;
			printf("left\n");
		}
		else if( (deltaX > 20) && (p.x < p.x_last) && (deltaX > deltaY) )
		{
			p.motion = TC_RIGHT;
			printf("right\n");
		}
		else if( (deltaY > 20) && (p.y > p.y_last) && (deltaX < deltaY) )
		{
			p.motion = TC_UP;
			printf("up\n");
		}
		else if( (deltaY > 20) && (p.y < p.y_last) && (deltaX < deltaY) )
		{
			p.motion = TC_DOWN;
			printf("down\n");
		}

	} 
	
	//关闭触摸屏 
	close(fd_ts);
}
#endif
/****************************************************************/

void Init_tcState(void)
{
	/* 初始化触摸屏数据结构体 */
	touch.response = true;	/* 初始化为已被响应 */
	touch.x_per = touch.y_per = 0;
	touch.x = touch.x_last = touch.y = touch.y_last = 0;
	touch.time_per = touch.time = touch.time_last = 0;
	touch.motion = TC_NONE;
	touch.state = STATE_NOT_PRESSED;
}

void Renew_tcState(void)
{
	touch.response = false;	/* 初始化未被响应 */
	touch.x_per = touch.x_last;
	touch.y_per = touch.y_last;
	touch.time_per = touch.time_last;

	touch.time =  touch.time_last = 0;
	touch.x = touch.x_last = touch.y = touch.y_last = 0;
	touch.motion = TC_NONE;
	touch.state = STATE_NOT_PRESSED;
}

void get_tcState(TOUCH *tc)
{
	memcpy(tc, &touch, sizeof(TOUCH));
}

void response_over()
{
	touch.response = true;
}

void *Touch_stateMachine(void *arg)
{
	bool read_flag = true;	
	bool start_judge_sin_or_dou = false;	/* 标志是否开始判断单双机 */
	struct input_event tc_data;
	u_int8_t waiting_count = 0;
	u_int8_t pressed_count = 0;
	u_int32_t read_count = 0;
	int 	fd_ts;	/* 触摸屏文件 */
		
	
	//打开触摸屏 
	fd_ts = open(TOUCH_PATH, O_RDONLY);
	if(fd_ts == -1)
	{
		perror("open touch");
		pthread_cancel(child_id);
		return NULL;
	}

	/* 初始化触摸屏数据结构体 */
	Init_tcState();

	printf("--------in to state-------\n");

	while(1)
	{
		memset(&tc_data, 0, sizeof(struct input_event));
		if( (read_flag) )
		{
			read_flag = false;
#ifdef TOUCH_DEBUG			
			/* 读取触摸屏文件的数据 */
			printf("------ waiting for touch -----\n");
#endif /* TOUCH_DEBUG */
			read(fd_ts, &tc_data, sizeof(struct input_event));
			read_count++;
		}
		else
		{
			read_flag = true;
		}
		
		/* 状态机的正式代码 */
		switch (touch.state)
		{
			case STATE_NOT_PRESSED:	/* 无操作 */
				/* 如果这项成立意味着 “无操作” 状态可能是从其它状态转换来的，
					比如从“单击”状态过来。其实从其它状态回归为“无操作”状态也是一种变化 */
				tc_time_cur = (float)(time(NULL) - time_start);
				//if( (touch.state)  && (tc_time_cur-touch.time_last > 0.3) )	
				if( (!touch.response) && (tc_time_cur-touch.time_last > 0.3) )	
				{
					touch.response = true;
					// if( start_judge_sin_or_dou )	/* 开始判断单击还是双击 */
					// {
					// 	float time_cur = time(NULL);
					// }
					break;
				}
				/* 7.11 add */
				for(int i=0; i<2; i++)
				{	
					read(fd_ts, &tc_data, sizeof(struct input_event));
				/* 发生触屏事件，且为x轴事件 */
				if(tc_data.type==EV_ABS && tc_data.code==ABS_X)
				{
					touch.x = tc_data.value*800/1024;//*800/1024;

					/* 下面转换时间的比较复杂，用户只需要知道最终得到的时间是个
						距离本线程创建到现在过了多少秒就行了（精确到毫秒） */
					touch.time = (tc_data.time.tv_sec - time_start)*1.0
											 + ((tc_data.time.tv_usec/1000)*1.0/1000);
				}

				/* 发生触摸屏事件，且为y轴事件 */
				else if(tc_data.type==EV_ABS && tc_data.code==ABS_Y)
				{
					touch.y = tc_data.value*480/600;//*480/600;

					/* 下面转换时间的比较复杂，用户只需要知道最终得到的时间是个
						距离本线程创建到现在过了多少秒就行了（精确到毫秒） */
					touch.time = (tc_data.time.tv_sec - time_start)*1.0
											 + ((tc_data.time.tv_usec/1000)*1.0/1000);
				}
				}

				/* 触摸屏每点击一次对应要读两次，先读x坐标再读y坐标 */
				if( touch.x != 0 && touch.y != 0)
				{
					touch.state = STATE_PRESSED;
					touch.response = false;
				}

#ifdef TOUCH_DEBUG				
				printf("\n------------------\n");
				printf("time_per:%0.3f\n", touch.time_per);
				printf("time:%0.3f\n", touch.time);
				printf("time_lase:%0.3f\n", touch.time_last);
				printf("NOT_PRESSED:(%d,%d)%d\n", touch.x_per, touch.y_per, read_count);
				printf("   					(%d,%d)\n", touch.x, touch.y);
				printf("            (%d,%d)\n", touch.x_last, touch.y_last);
#endif /* TOUCH_DEBUG */				
				break;

			case STATE_PRESSED:			/* 按下 */
				{
					// printf("\n------------------\n");
					// /* 按压触摸屏 */
					// if(tc_data.type==EV_KEY && tc_data.code==BTN_TOUCH && tc_data.value>0 )
					// {
					// 	printf("按压 %d\n", tc_data.value);
					// }
					// read(fd_ts, &tc_data, sizeof(struct input_event));
					/* 发生了滑动 */
					if(tc_data.type==EV_ABS)
					{
						/* 7.11 add */
						for(int i=0; i<2; i++)
						{	
							read(fd_ts, &tc_data, sizeof(struct input_event));
							/* x 轴事件 */
							if(tc_data.code==ABS_X)
							{
								touch.x_last = tc_data.value*800/1024;//*800/1024;

								/* 下面转换时间的比较复杂，用户只需要知道最终得到的时间是个
									距离本线程创建到现在过了多少秒就行了（精确到毫秒） */
								touch.time_last = (tc_data.time.tv_sec - time_start)*1.0
																	+ ((tc_data.time.tv_usec/1000)*1.0/1000);
							}

							/* y 轴事件 */
							else if(tc_data.code==ABS_Y)
							{
								/* 发生触摸屏事件，且为y轴事件 */
								touch.y_last = tc_data.value*480/600;//*480/600;

								/* 下面转换时间的比较复杂，用户只需要知道最终得到的时间是个
									距离本线程创建到现在过了多少秒就行了（精确到毫秒） */
								touch.time_last = (tc_data.time.tv_sec - time_start)*1.0
																	+ ((tc_data.time.tv_usec/1000)*1.0/1000);
							}
						}
							/* 7.10 add */
							if((touch.x_last != 0) && (touch.y_last != 0) && (touch.time_last-touch.time>0.2))
							{	/* 判断为拖动 */
								touch.state = STATE_DTAGING;
								touch.response = false;
							}

					}
					/* 松开触摸屏 */
					else if(tc_data.type==EV_KEY && tc_data.code==BTN_TOUCH && tc_data.value==0 )
					{
						printf("0x%x\t0x%x\t0x%x\n", tc_data.type, tc_data.code, tc_data.value);
						if(touch.x_last==0 || touch.y_last == 0)
						{
							touch.x_last = touch.x;
							touch.y_last = touch.y;
						}
						touch.time_last = (tc_data.time.tv_sec - time_start)*1.0
															+ ((tc_data.time.tv_usec/1000)*1.0/1000);
						/* 7.11 add */
						// printf("become release\n");									
						touch.state = STATE_RELEASE;
						touch.response = true;
					}
					// printf("STATE_PRESSED\n");/* 状态显示 */
#ifdef TOUCH_DEBUG					
					printf("\n------------------\n");
					printf("time_per:%0.3f\n", touch.time_per);
					printf("time:%0.3f\n", touch.time);
					printf("time_lase:%0.3f\n", touch.time_last);
					printf("STATE_PRESSED:(%d,%d)%d\n", touch.x_per, touch.y_per, read_count);
					printf("              (%d,%d)\n", touch.x, touch.y);
					printf("              (%d,%d)\n", touch.x_last, touch.y_last);
#endif /* TOUCH_DEBUG */
				}
				// printf("STATE_PRESSED:(%d,%d)%d\n", touch.x_last, touch.y_last, read_count);
				break;

			case STATE_RELEASE:			/* 释放 */
				{				
					// int time_ time_start
					tc_time_cur = (float)(time(NULL) - time_start);
					if( (touch.response)  && (tc_time_cur-touch.time_last > 0.3) )
					{
						Renew_tcState();
						printf("mabe bug\n");
						break;
					}
					int deltaX = (touch.x-touch.x_last > 0) ? (touch.x-touch.x_last) : (touch.x_last-touch.x);
					int deltaY = (touch.y-touch.y_last > 0) ? (touch.y-touch.y_last) : (touch.y_last-touch.y);

					/* 如果大于0.2s则认为释放之前的位移是“拖动”而不是划屏之类的 */
						// printf("%f\n", (touch.time_last - touch.time));
						// printf("x:%d  y:%d\n", deltaX, deltaY);
					if( touch.time_last - touch.time < 1.2)	
					{
						/* 7.11 add */
						// printf("time ok\n");
						/* 判断划屏 */
						if( (deltaX > 30) && (touch.x > touch.x_last) && (deltaX > deltaY) )
						{
							printf("left\n");
							touch.motion = TC_LEFT;
							touch.state = STATE_SLIDING;
							touch.response = false;
						}
						else if( (deltaX > 30) && (touch.x < touch.x_last) && (deltaX > deltaY) )
						{
							printf("right\n");
							touch.motion = TC_RIGHT;
							touch.state = STATE_SLIDING;
							touch.response = false;
						}
						else if( (deltaY > 30) && (touch.y > touch.y_last) && (deltaX < deltaY) )
						{
							printf("up\n");
							touch.motion = TC_UP;
							touch.state = STATE_SLIDING;
							touch.response = false;
						}
						else if( (deltaY > 30) && (touch.y < touch.y_last) && (deltaX < deltaY) )
						{
							printf("down\n");
							touch.motion = TC_DOWN;
							touch.state = STATE_SLIDING;
							touch.response = false;
						}

						
						else if( (deltaX < 20) && (deltaY < 20) )
						{
							// touch.state = STATE_SIN_OR_DOU;
							// touch.response = false;
							/* 判断单击 */
							if(touch.time_last - touch.time_per > 0.1)
							{
								touch.state = STATE_SINGLE_CLICK;
								touch.response = false;
							}

							// /* 判断双击 */
							// else if(touch.time_last - touch.time_per < 0.8)
							// {
							// 	touch.state = STATE_DOUBLE_CLICK;
							// 	touch.response = false;
							// }
						}
					}
					else
					{
						/* 7.11 add */
						printf("become not\n");
						Renew_tcState();
					//	printf("拖动或者长按\n");
					}
				}
#ifdef TOUCH_DEBUG			
				printf("\n------------------\n");
				printf("time_per:%0.3f\n", touch.time_per);
				printf("time:%0.3f\n", touch.time);
				printf("time_lase:%0.3f\n", touch.time_last);
				printf("STATE_RELEASE:(%d,%d)%d\n", touch.x_per, touch.y_per, read_count);
				printf("              (%d,%d)\n", touch.x, touch.y);
				printf("              (%d,%d)\n", touch.x_last, touch.y_last);
#endif /* TOUCH_DEBUG */				
				break;

#if 0
			case STATE_SIN_OR_DOU:	/* 判断单击还是双击 */
				// {
				// 	// pthread_t id;
				// 	// pthread_create(&id, NULL, TouchGet, NULL);
				// 	// usleep(80000);		/* 延时0.5s */
				// 	// pthread_cancel(id);

				// 	int deltaX = (touch.x-touch.x_last > 0) ? (touch.x-touch.x_last) : (touch.x_last-touch.x);
				// 	int deltaY = (touch.y-touch.y_last > 0) ? (touch.y-touch.y_last) : (touch.y_last-touch.y);
				// 	if(( is_double_click == true) && (deltaX < 20) && (deltaY < 20))
				// 	{
				// 		is_double_click = false;
				// 		/* 是双击 */
				// 		touch.state = STATE_DOUBLE_CLICK;
				// 		touch.response = false;
				// 	}
				// 	else
				// 	{
				// 		/* 是单击 */
				// 		touch.state = STATE_SINGLE_CLICK;
				// 		touch.response = false;
				// 	}
				// }

				if( !start_judge_sin_or_dou )
				{
					start_judge_sin_or_dou = true;
					Renew_tcState();
				}
				else
				{
					start_judge_sin_or_dou = false;
					// /* 判断单击 */
					if(touch.time_last - touch.time_per > 0.8)
					{
						touch.state = STATE_SINGLE_CLICK;
						touch.response = false;
					}

					/* 判断双击 */
					else if(touch.time_last - touch.time_per < 0.8)
					{
						touch.state = STATE_DOUBLE_CLICK;
						touch.response = false;
					}
				}

				printf("\n------------------\n");
				printf("time_per:%0.3f\n", touch.time_per);
				printf("time:%0.3f\n", touch.time);
				printf("time_lase:%0.3f\n", touch.time_last);
				printf("STATE_SIN_OR_DOU:(%d,%d)%d\n", touch.x_per, touch.y_per, read_count);
				printf("                 (%d,%d)\n", touch.x, touch.y);
				printf("                 (%d,%d)\n", touch.x_last, touch.y_last);
				break;
#endif
			case STATE_SINGLE_CLICK:/* 单击 */
				/* 该状态已经被响应,回归“无操作”状态 */
				if( touch.response )
				{
					printf("responed single click\n");
					// sleep(1);	
					// usleep(2000);
					Renew_tcState();
				}
				// printf("STATE_SINGLE_CLICK\n");	/* 状态显示 */
#ifdef TOUCH_DEBUG 
				printf("\n------------------\n");
				printf("time_per:%0.3f\n", touch.time_per);
				printf("time:%0.3f\n", touch.time);
				printf("time_lase:%0.3f\n", touch.time_last);
				printf("STATE_SINGLE_CLICK:(%d,%d)%d\n", touch.x_per, touch.y_per, read_count);
				printf("                   (%d,%d)\n", touch.x, touch.y);
				printf("                   (%d,%d)\n", touch.x_last, touch.y_last);
				if(touch.state == STATE_NOT_PRESSED)
				{
					printf("single to not\n");
				}
				break;
#endif /* TOUCH_DEBUG */
			case STATE_DOUBLE_CLICK:/* 双击 */
				/* 该状态已经被响应,回归“无操作”状态 */
				if( touch.response )
				{ 
					// sleep(1);		
					// usleep(2000);	
					Renew_tcState();
				}
#ifdef TOUCH_DEBUG				
				printf("\n------------------\n");
				printf("time_per:%0.3f\n", touch.time_per);
				printf("time:%0.3f\n", touch.time);
				printf("time_lase:%0.3f\n", touch.time_last);
				printf("STATE_DOUBLE_CLICK:(%d,%d)%d\n", touch.x_per, touch.y_per, read_count);
				printf("                   (%d,%d)\n", touch.x, touch.y);
				printf("                   (%d,%d)\n", touch.x_last, touch.y_last);
#endif /* TOUCH_DEBUG */				
				break;

			case STATE_SLIDING:		  /* 滑屏 */
				/* 该状态已经被响应,回归“无操作”状态 */
				if( touch.response )
				{
					// sleep(1);	
					// usleep(2000);
					Renew_tcState();
				}
				// printf("STATE_SLIDING\n");/* 状态显示 */
#ifdef TOUCH_DEBUG				
				printf("\n------------------\n");
				printf("time_per:%0.3f\n", touch.time_per);
				printf("time:%0.3f\n", touch.time);
				printf("time_lase:%0.3f\n", touch.time_last);
				printf("STATE_SLIDING:(%d,%d)%d\n", touch.x_per, touch.y_per, read_count);
				printf("              (%d,%d)\n", touch.x, touch.y);
				printf("              (%d,%d)\n", touch.x_last, touch.y_last);
#endif /* TOUCH_DEBUG */		
				break;
			
			/* 7.10 add */
			case STATE_DTAGING:		  /* 拖动 */
				/* 该状态已经被响应,回归“无操作”状态 */
				// if( touch.response )
				// {
				// 	sleep(1);	
				// 	Renew_tcState();
				// }
				/* 发生了滑动 */
					if(tc_data.type==EV_ABS)
					{
						/* x 轴事件 */
						if(tc_data.code==ABS_X)
						{
							touch.x_last = tc_data.value*800/1024;//*800/1024;

							/* 下面转换时间的比较复杂，用户只需要知道最终得到的时间是个
								距离本线程创建到现在过了多少秒就行了（精确到毫秒） */
							touch.time_last = (tc_data.time.tv_sec - time_start)*1.0
																+ ((tc_data.time.tv_usec/1000)*1.0/1000);
						}

						/* y 轴事件 */
						else if(tc_data.code==ABS_Y)
						{
							/* 发生触摸屏事件，且为y轴事件 */
							touch.y_last = tc_data.value*480/600;//*480/600;

							/* 下面转换时间的比较复杂，用户只需要知道最终得到的时间是个
								距离本线程创建到现在过了多少秒就行了（精确到毫秒） */
							touch.time_last = (tc_data.time.tv_sec - time_start)*1.0
																+ ((tc_data.time.tv_usec/1000)*1.0/1000);
						}
						/* 7.10 add */
						// if((touch.x_last != 0) && (touch.y_last != 0) && (touch.time_last-touch.time>0.2))
						// {	/* 判断为拖动 */
						// 	touch.state = STATE_DTAGING;
						// 	touch.response = false;
						// }

					}
					/* 松开触摸屏 */
					else if(tc_data.type==EV_KEY && tc_data.code==BTN_TOUCH && tc_data.value==0 )
					{
						// if(touch.x_last==0 && touch.y_last == 0)
						// {
						// 	touch.x_last = touch.x;
						// 	touch.y_last = touch.y;
						// }
						touch.time_last = (tc_data.time.tv_sec - time_start)*1.0
															+ ((tc_data.time.tv_usec/1000)*1.0/1000);
						touch.state = STATE_RELEASE;
						touch.response = false;
					}
				// printf("STATE_DTAGING\n");/* 状态显示 */
#ifdef TOUCH_DEBUG					
				printf("\n------------------\n");
				printf("time_per:%0.3f\n", touch.time_per);
				printf("time:%0.3f\n", touch.time);
				printf("time_lase:%0.3f\n", touch.time_last);
				printf("STATE_DTAGING:(%d,%d)%d\n", touch.x_per, touch.y_per, read_count);
				printf("              (%d,%d)\n", touch.x, touch.y);
				printf("              (%d,%d)\n", touch.x_last, touch.y_last);
#endif /* TOUCH_DEBUG */				
				break;

			default:
				break;
		}

		// usleep(10000);
	}
}

/*------------------------------ end of file --------------------------------*/

